﻿Imports System.IO
Imports System.Data.OleDb
Public Class Form1
    Dim con As New OleDbConnection()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*"
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.PictureBox1.Image = System.Drawing.Image.FromFile(ofd.FileName)
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=.\Database1.accdb;"
        con.Open()
        'MessageBox.Show("Success")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Dim MemStream As New MemoryStream
        'Dim DataPic_Update As Byte()
        'Me.PictureBox1.Image.Save(MemStream, Imaging.ImageFormat.Png)
        'DataPic_Update = MemStream.GetBuffer()
        'MemStream.Read(DataPic_Update, 0, MemStream.Length)
        'Dim cmd As OleDbCommand = New OleDbCommand("insert into tests (photo) values(@EmpPic)", con)
        '' image content
        'Dim photo As OleDbParameter = New OleDbParameter("@EmpPic", SqlDbType.Image)
        'photo.Value = DataPic_Update
        'cmd.Parameters.Add(photo)
        'cmd.ExecuteNonQuery()
        'MessageBox.Show("Record Is Added")
        Dim MemStream As New MemoryStream
        Dim DataPic_Update As Byte()
        Me.PictureBox1.Image.Save(MemStream, Imaging.ImageFormat.Png)
        DataPic_Update = MemStream.GetBuffer()
        MemStream.Read(DataPic_Update, 0, MemStream.Length)
        cmdp.Parameters("@EmpPic") = DataPic_Update
        cmdp.Parameters("@any") = txt1.Text
        cmdp.ExecuteNonQuery("insert into tests (photo,Nany) values(@EmpPic,@any)")
        MessageBox.Show("Record Is Added")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            Dim cmd As OleDb.OleDbCommand
            Dim dr As OleDb.OleDbDataReader
            cmd = con.CreateCommand()
            cmd.CommandText = "SELECT Photo FROM Employee WHERE EmpId=83"

            dr = cmd.ExecuteReader

            If dr.Read Then
                Dim bytImage() As Byte
                Try
                    bytImage = CType(dr(0), Byte())
                    Dim ms As New System.IO.MemoryStream(bytImage)
                    Dim bmImage As New Bitmap(ms)
                    ms.Close()

                    PictureBox1.Image = bmImage
                    PictureBox1.Refresh()
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End If

            dr.Close()
            con.Close()

            cmd.Dispose()
            con.Dispose()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim dt1 As Date = "06-05-1988"
        Dim dt2 As Date = Now.ToShortDateString
        Dim dt3 As TimeSpan = (dt2 - dt1)
        Dim diff As Double = dt3.Days
        Dim sAge As String

        sAge = Str(Int(diff / 365)) & " Year "
        diff = diff Mod 365
        sAge = sAge & Str(Int(diff / 30)) & " Month(s)"
        diff = diff Mod 30
        sAge = sAge & Str(diff) & " Day(s)"

        MessageBox.Show(sAge)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim i = 3 Mod 2
        MessageBox.Show(i)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "pdf|*.pdf"
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtpath.Text = ofd.FileName
        End If
    End Sub

    Private Sub txtcopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncopy.Click
        Dim fp = ".\pdf\" & txtrename.Text & ".pdf"
        My.Computer.FileSystem.CopyFile(txtpath.Text, fp, True)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lbltime.Text = TimeOfDay
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim a = 44
        cmd.ExecuteNonquery("UPdate Employee SET EmpName='" & txt1.Text & "'WHERE EmpId= '" & a & "' ")
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

        'cmdp.Parameters("@empno") = txtno.Text
        cmdp.Parameters("@name") = txt1.Text
        cmdp.Parameters("@id") = 44
        cmdp.ExecuteNonQuery("UPdate Employee SET EmpName=@name WHERE EmpId=@id ")
        MessageBox.Show("កែប្រែពត៌មានមន្រ្តីជោកជ័យ")
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        cmdp.Parameters("@name") = "Test"
        cmdp.Parameters("@pwd") = 123
        cmdp.Parameters("@rid") = 2
        cmdp.ExecuteNonQuery("INSERT INTO UserTbl ([Username],[Password],UserRoleId) Values(@name,@pwd)")
        MessageBox.Show("Success")
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        System.IO.File.Delete(".\pdf\1254ឋានៈ.pdf")
        MsgBox("Success")
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Try
            Dim cmd As OleDb.OleDbCommand
            Dim dr As OleDb.OleDbDataReader
            cmd = con.CreateCommand()
            cmd.CommandText = "SELECT photo FROM tests WHERE id=13"

            dr = cmd.ExecuteReader
            'Dim b As Object
            If dr.Read Then
                Dim bytImage() As Byte
                Try
                    bytImage = CType(dr(0), Byte())
                    Dim ms As New System.IO.MemoryStream(bytImage)
                    Dim bmImage As New Bitmap(ms)
                    ms.Close()
                    PictureBox1.Image = bmImage
                    PictureBox1.Refresh()
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End If

            dr.Close()
            con.Close()

            cmd.Dispose()
            con.Dispose()
            'System.Diagnostics.Process.Start(b)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        cmdp.ExecuteNonQuery("UPDATE Employee SET WorkRetire=1,Active=1")
    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
       
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Dim ti = Now.Year
        MessageBox.Show(ti)
    End Sub
End Class
